// pages/integral/integral.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const jumpWeapp = utils.appid;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        jlist: [],
        m_uid: '',
        jf: "",
        show_jf: '',
        show_jf50: '',
        unit: '',
        weapp_status: 0,
        optionL: '',
        s_login: false,
        official: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        app.ifuserinfo();
        var reg = RegExp(/testweapp.whwangdoudou.cn/);
        if (reg.exec(utils.url)) {
            that.setData({
                official: true
            })
        };
        that.setData({
            optionL: options,
            jumpWeapp: jumpWeapp
        })

        let p0 = {
            m: 'weapp_status',
            v: 10
        };
        post(p0).then(res => {
            that.setData({
                weapp_status: res.status
            });

            if (1 == that.data.weapp_status) { // 审核通过了 
                wx.showToast({
                    icon: 'loading',
                    duration: 500
                });
                that.doGetUserInfo(options);
            }
        });

    },
    doGetUserInfo(options) {
        let that = this;
        var _userinfo = wx.getStorageSync('_userinfo');
        var weapp_openid = wx.getStorageSync('weapp_openid');
        if (_userinfo && weapp_openid && weapp_openid == _userinfo.openId) {
            that.doLogin(options);
        } else {
            // 获取用户信息

        }
    },
    doLogin(params) {
        let that = this;
        var z_uid = wx.getStorageSync("j_uid"); // 对应赚客login_id
        var m_uid = wx.getStorageSync("jm_uid"); // 对应赚客用户uid
        if (z_uid && m_uid) {
            that.onShow();
        } else {
            let p = {};
            var channel = params.channel; //渠道号
            if (!channel) {
                wx.showModal({
                    content: '渠道为空，无法使用',
                    showCancel: false
                });

                return;
            }
            var third_uid = params.third_uid; //用户的uid
            if (!third_uid) {
                wx.showModal({
                    content: '第三方用户uid为空，无法使用',
                    showCancel: false
                });
                return;
            }

            p = {
                m: 'j_login',
                channel: channel,
                third_uid: third_uid
            };

            var _userinfo = wx.getStorageSync('_userinfo');
            var weapp_openid = wx.getStorageSync('weapp_openid');
            if (_userinfo && weapp_openid && weapp_openid == _userinfo.openId) {
                p.userinfo = JSON.stringify(_userinfo);
            }
            post(p).then(res => {
                if (0 == res.r) {
                    wx.setStorageSync("j_uid", res.uid); // 对应赚客手机号
                    wx.setStorageSync("jm_uid", res.m_uid); // 对应赚客用户uid
                    that.setData({
                        s_login: true
                    })
                    that.onShow();
                }
            });
        }
    },

    tapId(event) {
        let that = this;
        // let url = '/pages/skip/skip?uid=' + that.data.m_uid + '&weappid=' + event.target.dataset.weappid + '&from=jlist' + '&jfplay=' + event.target.dataset.jfplay;
        let url = '/pages/jump/jump?uid=' + that.data.m_uid + '&weappid=' + event.target.dataset.weappid + '&from=jlist' + '&jfplay=' + event.target.dataset.jfplay;
        wx.navigateTo({
            url: url
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        app.getLbs();
        var z_uid = wx.getStorageSync("j_uid"); // 对应赚客手机号
        var m_uid = wx.getStorageSync("jm_uid"); // 对应赚客用户uid
        if (z_uid && m_uid) {
            that.setData({
                z_uid: z_uid,
                m_uid: m_uid
            })
            that.listData()
        } else {
            that.doGetUserInfo(that.data.optionL)
        }
    },

    listData() {
        let that = this;
        let p = {
            m: 'jindex',
            juid: that.data.z_uid,
            ua: app.globalData.ua
        };
        post(p).then(res => {
            if (res.show_jf == 1) {
                that.setData({
                    show_jf50: 100,
                    show_jf: res.show_jf
                });
            } else {
                that.setData({
                    show_jf50: 0,
                    show_jf: res.show_jf
                });
            }
            that.setData({
                m_uid: res.m_uid,
                jf: res.jf,
                jlist: res.jlist,
                unit: res.unit
            });
            // 跳转到jump
            // console.log(res.jlist)
            // for (var i = 0; i < res.jlist.length; i++) {
            //     if (res.jlist[i].jf_play == 0) {
            //         if (that.data.s_login) {
            //             var uidS = wx.getStorageSync('jm_uid');
            //             wx.navigateTo({
            //                 url: '../../pages/jump/jump?uid=' + uidS + '&weappid=' + res.jlist[i].weappid
            //             });
            //             that.data.s_login = !that.data.s_login
            //             break;
            //         }
            //     }
            // }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function() {

    },*/
    submitFormId(e) {
        // console.log(e);
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            uid: that.data.m_uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        // console.log(p)
        post(p).then(res => {})
    }
})